package com.example.park_2306

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.DateEntity
import com.example.base.entity.DateVisitEntity
import com.example.base.mvi.BaseActivity
import com.example.base.mvi.BaseViewActivity
import com.example.base.mvi.HttpType
import com.example.base.mvi.UiState
import com.example.park_2306.adapter.DateAdapter
import com.example.park_2306.databinding.ActivityDateBinding
import com.example.park_2306.date.DateIntent
import com.example.park_2306.date.DateViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import java.util.Calendar

@Route(path = "/app/DateActivity")
@AndroidEntryPoint
class DateActivity : BaseActivity<ActivityDateBinding,DateIntent,DateViewModel>() {

    val dateAdapter by lazy { DateAdapter() }
    val dateList = mutableListOf<DateEntity>()
    override fun getLayout(): Int = R.layout.activity_date
    override fun getVM(): Class<DateViewModel> = DateViewModel::class.java

    override fun initView() {
        //循环添加数据
        var calendar = Calendar.getInstance()
        for (i in 0..6){
            calendar.add(Calendar.DAY_OF_MONTH,i)
            dateList.add(DateEntity(calendar.time,if(i==0) true else false))
            calendar = Calendar.getInstance()
        }
        binding.rvDate.apply {
            adapter= dateAdapter
            layoutManager = GridLayoutManager(this@DateActivity,7)
        }
        dateAdapter.data.addAll(dateList)
        dateAdapter.notifyDataSetChanged()

        //点击切换对应日期
        dateAdapter.setOnItemClickListener { adapter, view, position ->
            dateAdapter.data.forEach {
                it.isChecked = false
            }
            dateAdapter.data[position].isChecked = true
            dateAdapter.notifyDataSetChanged()
        }

        binding.btnInsert.setOnClickListener {
            val dateEntity = dateAdapter.data.filter { it.isChecked } as MutableList<DateEntity>
            viewModel.sendIntent(DateIntent.insert(DateVisitEntity(0,dateEntity[0].content.toString())))
        }
        binding.btnQuery.setOnClickListener {
            viewModel.sendIntent(DateIntent.query)
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            viewModel.uiState.collect{
                when(it){

                    is UiState.onSuccess<*> ->
                    {when(it.type){
                        HttpType.DEFAULT -> {
                            ToastUtils.showLong("陈工")
                        }

                        HttpType.DETAIL -> {
                            val dateVisitEntities = it.data as MutableList<DateVisitEntity>
                            ToastUtils.showLong(dateVisitEntities.size)
                        }
                       else->{}
                    }}
                    else->{

                    }
                }
            }
        }
    }

}